package com.edu.jsu.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.jsu.commonutils.R;
import com.edu.jsu.eduservice.entity.Lecture;
import com.edu.jsu.eduservice.entity.vo.LectureQuery;
import com.edu.jsu.eduservice.service.LectureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * <p>
 * 讲师表 前端控制器
 * </p>
 *
 * @author 廖俊杰
 * @since 2021-11-29
 */
@RestController
@RequestMapping("/eduservice/lecture")
@Api(tags = "讲师管理")
public class LectureController {

    @Resource
    private LectureService lectureService;

    @ApiOperation(value = "查询所有讲师列表")
    @GetMapping("/findAll")
    private R findAllLectures() {
        List<Lecture> list = lectureService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R removeLecture(@PathVariable String id) {
        boolean flag = lectureService.removeById(id);
        return flag == true ? R.ok() : R.error();
    }

    /**
     * 分页查询讲师
     *
     * @param current 当前页
     * @param limit   每页记录数
     * @return
     */
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/pageLecture/{current}/{limit}")
    public R pageListLecture(@PathVariable Long current,
                             @PathVariable Long limit) {

        Page<Lecture> pageLecture = new Page<>(current, limit);
        IPage<Lecture> page = lectureService.page(pageLecture, null);

        //总记录数
        long total = pageLecture.getTotal();
        //数据list集合
        List<Lecture> records = pageLecture.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 条件查询带分页
     *
     * @param current      当前页
     * @param limit        每页记录数
     * @param lectureQuery 查询条件
     * @return
     */
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/pageLectureCondition/{current}/{limit}")
    public R pageLectureCondition(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) LectureQuery lectureQuery) {
        Page<Lecture> pageLecture = new Page<>(current, limit);
        QueryWrapper<Lecture> wrapper = new QueryWrapper<>();

        String name = lectureQuery.getName();
        Integer level = lectureQuery.getLevel();
        String begin = lectureQuery.getBegin();
        String end = lectureQuery.getEnd();

        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");
        lectureService.page(pageLecture, wrapper);
        //总记录数
        long total = pageLecture.getTotal();
        //数据list集合
        List<Lecture> records = pageLecture.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("/addLecture")
    public R addLecture(@RequestBody Lecture lecture) {
        boolean save = lectureService.save(lecture);
        return save ? R.ok() : R.error();
    }


    @ApiOperation(value = "根据讲师id查询")
    @GetMapping("/getLecture/{id}")
    public R getLecture(@PathVariable String id){
        Lecture lecture = lectureService.getById(id);

        return R.ok().data("lecture",lecture);
    }

    @ApiOperation(value = "讲师修改")
    @PostMapping("/updateLecture")
    public R updateLecture(@RequestBody Lecture lecture) {
        boolean flag = lectureService.updateById(lecture);

        return flag ? R.ok() : R.error();
    }
}

